﻿#region Copyright © 2011 Prageeth Silva <prageeth@thenewcoders.com>
/*
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the author(s) be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 *   1. The origin of this software must not be misrepresented; you must not
 *      claim that you wrote the original software. If you use this software
 *      in a product, an acknowledgment in the product documentation would be
 *      appreciated but is not required.
 * 
 *   2. Altered source versions must be plainly marked as such, and must not
 *      be misrepresented as being the original software.
 * 
 *   3. This notice may not be removed or altered from any source distribution.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SourcePlus.Core;
using SourcePlus.Themes;

namespace SourcePlus.Ui
{
    public partial class frmThemes : Form
    {

        private BatchAssembler assembler = new BatchAssembler();

        public SourceTheme SelectedTheme
        {
            get { return this.assembler.Theme; } 
            set
            { 
                this.assembler.Theme = value;
                if (value != null)
                {
                    this.textUpdate = true;
                    this.cmbTheme.Text = value.ToString();
                    this.textUpdate = false;
                }
            } 
        }

        private bool textUpdate = false;

        private SampleCode[] samples = null;
        
        public frmThemes(SourceTheme theme)
        {

            InitializeComponent();
                                    
            string path = (string.IsNullOrEmpty(Settings.ThemesExamplePath)) ? Application.StartupPath : Settings.ThemesExamplePath;
            this.samples = SampleCode.GetSamples(path);

            
            this.cmbTheme.SuspendLayout();
            this.cmbTheme.Items.Clear();
            foreach (SourceTheme stheme in Settings.Themes)
            {
                this.cmbTheme.Items.Add(stheme);
            }
            this.cmbTheme.ResumeLayout();

            this.SelectedTheme = theme;
            

            this.cmbSamples.SuspendLayout();
            this.cmbSamples.Items.Clear();
            foreach (SampleCode sample in this.samples)
            {
                this.cmbSamples.Items.Add(sample);
            }
            if (this.cmbSamples.Items.Count > 0) { this.cmbSamples.SelectedIndex = 0; }
            this.cmbSamples.ResumeLayout();
            
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.SelectedTheme = null;
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void cmbSamples_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateSample();
        }

        private void cmbTheme_SelectedIndexChanged(object sender, EventArgs e)
        {

            // skip manual text updates
            if (this.textUpdate) { return; }
            
            try
            {
                this.SelectedTheme = this.cmbTheme.SelectedItem as SourceTheme;                
                this.UpdateSample();
            }
            catch { }

        }

        private void UpdateSample()
        {

            if (this.cmbSamples.SelectedItem == null) { return; }

            SampleCode sample = this.cmbSamples.SelectedItem as SampleCode;

            if (sample == null) { return; }

            this.assembler.FormatFile(this.rtbSample, sample.File, BatchAssembler.GetFormatterByType(sample.Name));

        }

        private class SampleCode
        {

            public string Name { get; set; }

            public string File { get; set; }

            public SampleCode(string name, string file)
            {
                this.Name = name;
                this.File = file;
            }

            public override string ToString()
            {
                return this.Name;
            }

            public static SampleCode[] GetSamples(string dir)
            {

                DirectoryAnalyser analyser = new DirectoryAnalyser();
                string[] files = analyser.GetFilesList(dir, new string[] { "sample" }, false, true);
                List<SampleCode> samples = new List<SampleCode>(files.Length);

                foreach (string file in files)
                {
                    try
                    {
                        string name = file.Substring(file.IndexOf('\\') + 1);
                        name = name.Substring(0, name.LastIndexOf('.')).ToUpper();
                        samples.Add(new SampleCode(name, file));
                    }
                    catch { continue; }
                }

                return samples.ToArray();

            }
            
        }
                
    }

}
